﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using RoAuthorizationServer;

namespace AuthorizationServer
{
  public class Startup
  {
    public Startup(IConfiguration configuration)
    {
      Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
      services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
      // 1.设置IdentityServer
      services.AddIdentityServer()
        .AddDeveloperSigningCredential()
        .AddInMemoryApiResources(IdentityServerConfig.GetApis())
        .AddInMemoryClients(IdentityServerConfig.GetClients())
        //        AddTestUsers 方法帮我们做了以下几件事：
        //为资源所有者密码授权添加支持
        //添加对用户相关服务的支持，这服务通常为登录 UI 所使用（我们将在下一个快速入门中用到登录 UI）
        //为基于测试用户的身份信息服务添加支持（你将在下一个快速入门中学习更多与之相关的东西）
        .AddTestUsers(IdentityServerConfig.GetUsers())
        //添加自定义ProfileService
        .AddProfileService<CustomProfileService>()
        //添加密码授权模式自定义验证器
        .AddResourceOwnerValidator<CustomResourceOwnerPasswordValidator>();

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
      if (env.IsDevelopment())
      {
        app.UseDeveloperExceptionPage();
      }

      // 2.使用IdentityServer 中间件
      app.UseIdentityServer();
      app.UseMvc();
    }
  }
}
